package com.aruba.animationlibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 自定义动画框架使用的LinearLayout
 */
public class AnimatorLinearLayout extends LinearLayoutCompat {

    public AnimatorLinearLayout(Context context) {
        this(context, null);
    }

    public AnimatorLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
    }

    /**
     * 解析自定义属性，并记录下来，用于之后addView使用
     *
     * @param attrs
     * @return
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MyLayoutParams(getContext(), attrs);
    }

    /**
     * 干预Activity启动过程中xml解析，偷梁换柱使用自定义动画组件包裹
     *
     * @param child
     * @param params
     */
    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        MyLayoutParams p = (MyLayoutParams) params;
        if (!isDiscrollvable(p)) {
            super.addView(child, params);
        } else {
            //将原始child中的属性给AnimatorFrameLayout用
            AnimatorFrameLayout animatorFrameLayout = new AnimatorFrameLayout(getContext());
            animatorFrameLayout.setmDiscrollveAlpha(p.mDiscrollveAlpha);
            animatorFrameLayout.setmDiscrollveFromBgColor(p.mDiscrollveFromBgColor);
            animatorFrameLayout.setmDiscrollveToBgColor(p.mDiscrollveToBgColor);
            animatorFrameLayout.setmDiscrollveScaleX(p.mDiscrollveScaleX);
            animatorFrameLayout.setmDisCrollveTranslation(p.mDisCrollveTranslation);
            animatorFrameLayout.setmDiscrollveScaleY(p.mDiscrollveScaleY);
            //使得child成为AnimatorFrameLayout的子View
            animatorFrameLayout.addView(child);

            //将AnimatorFrameLayout添加到AnimatorLinearLayout中
            super.addView(animatorFrameLayout, params);
        }
    }

    private boolean isDiscrollvable(MyLayoutParams p) {
        return p.mDiscrollveAlpha ||
                p.mDiscrollveScaleX ||
                p.mDiscrollveScaleY ||
                p.mDisCrollveTranslation != -1 ||
                (p.mDiscrollveFromBgColor != -1 &&
                        p.mDiscrollveToBgColor != -1);
    }

    public class MyLayoutParams extends LinearLayoutCompat.LayoutParams {
        public int mDiscrollveFromBgColor;//背景颜色变化开始值
        public int mDiscrollveToBgColor;//背景颜色变化结束值
        public boolean mDiscrollveAlpha;//是否需要透明度动画
        public int mDisCrollveTranslation;//平移值
        public boolean mDiscrollveScaleX;//是否需要x轴方向缩放
        public boolean mDiscrollveScaleY;//是否需要y轴方向缩放

        public MyLayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            //解析attrs得到自定义的属性，保存
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DiscrollView_LayoutParams);
            mDiscrollveAlpha = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_alpha, false);
            mDiscrollveScaleX = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_scaleX, false);
            mDiscrollveScaleY = a.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_scaleY, false);
            mDisCrollveTranslation = a.getInt(R.styleable.DiscrollView_LayoutParams_discrollve_translation, -1);
            mDiscrollveFromBgColor = a.getColor(R.styleable.DiscrollView_LayoutParams_discrollve_fromBgColor, -1);
            mDiscrollveToBgColor = a.getColor(R.styleable.DiscrollView_LayoutParams_discrollve_toBgColor, -1);
            a.recycle();
        }
    }

}
